package com.zsc.edu.gateway.modules.operationLog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.modules.operationLog.entity.OperationLog;
import com.zsc.edu.gateway.modules.operationLog.entity.OperationLogAnnotation;
import com.zsc.edu.gateway.modules.operationLog.query.OperationLogQuery;
import com.zsc.edu.gateway.modules.operationLog.repo.OperationLogRepository;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhuang
 */
@AllArgsConstructor
@RestController
@RequestMapping("/api/rest/log")
public class OperationLogController {
    private OperationLogRepository repo;

    /**
     * 获取操作日志详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('operationLog:query')")
    public OperationLog crate(@PathVariable("id") Long id) {
        return repo.selectById(id);
    }

    /**
     * 获取操作日志分页
     */
    @GetMapping("")
    @PreAuthorize("hasAuthority('operationLog:query')")
    public Page<OperationLog> query(Page<OperationLog> page, OperationLogQuery query) {
        return repo.selectPage(page, query.wrapper());
    }

    /**
     * 批量删除操作日志
     */
    @DeleteMapping("/batch")
    @PreAuthorize("hasAuthority('operationLog:delete')")
    @OperationLogAnnotation(content = "'批量操作日志'", operationType = "删除")
    public int deleteBatch(@RequestBody List<Long> ids) {
        LambdaQueryWrapper<OperationLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(OperationLog::getId, ids);
        return repo.delete(wrapper);
    }

}
